'use strict';
module.exports = {
	/**
	 * 关注/取消操作
	 * @url client/user/kh/attention 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} author_id  创作者id
	 * @param {String} status 1关注 2取消
	 * @param {String} nickname 昵称（关注必传/取消可不传）
	 * @param {String} avatar 头像（关注必传/取消可不传）
	 * @param {String} intro 简介（关注必传/取消可不传）
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------
		let { author_id, status, nickname, avatar, intro } = data;
		
		if(vk.pubfn.isNull(status)) {
			return { code: -1, msg: "Invalid status parameter" }
		}
		
		let dbName = "db-attention";
		
		let dbRes = await vk.baseDao.findByWhereJson({
			dbName,
			whereJson: {
				author_id: author_id,
				uid: userInfo._id
			}
		})
		
		if(!vk.pubfn.isNull(dbRes)) {
			if(status !== 2) {
				return { code: -1, msg: "Invalid status parameter"}
			}
			res["data"] = await vk.baseDao.deleteById({
				dbName,
				id: dbRes._id
			})
			let delRes = await vk.baseDao.del({
				dbName: "db-fans",
				whereJson: {
					author_id: dbRes.uid,
					uid: author_id
				}
			})
		} else {
			if(status !== 1) {
				return { code: -1, msg: "Invalid status parameter"}
			}
			res["data"] = await vk.baseDao.add({
				dbName,
				dataJson: {
					author_id: author_id,
					nickname: nickname,
					avatar: avatar,
					intro: intro,
					uid: userInfo._id,
					status: status
				}
			})
			let num = await vk.baseDao.add({
				dbName: "db-fans",
				dataJson: {
					author_id: userInfo._id,
					nickname: userInfo.nickname,
					avatar: userInfo.avatar,
					intro: userInfo.intro,
					uid: author_id,
					status: status
				}
			})
		}
		
		delete res["data"];
		
		res["msg"] = status !== 2? "关注成功": "取消成功";
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
